<template>
  <div style="max-width: 1280px; margin: 0 auto">
    <div>
      <h1>题目管理界面</h1>
    </div>
    <a-form :model="listParams" :layout="layout">
      <a-form-item field="title" label="名称">
        <a-input
          v-model="listParams.title"
          placeholder="请输入题目名称"
          style="min-width: 240px"
        />
      </a-form-item>
      <a-form-item field="tags" label="标签">
        <a-input-tag
          v-model="listParams.tags"
          placeholder="按回车生成单个标签"
          style="min-width: 240px"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="serchQuestion">搜索</a-button>
      </a-form-item>
      <a-divider size="0" />
    </a-form>
    <a-table
      :columns="columns"
      :data="list"
      :pagination="{
        showTotal: true,
        current: listParams.current,
        pageSize: listParams.pageSize,
        totalNum,
      }"
      @page-change="onPageChange"
    >
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="practice(record.id)">练习</a-button>
        </a-space>
      </template>
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag v-for="(tag, index) of record.tags" :key="index" color="green"
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #submitRate="{ record }">
        {{
          `${record.submitNum ? record.acceptNum / record.submitNum : "0"}% (${
            record.acceptNum
          } / ${record.submitNum})`
        }}
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, watchEffect } from "vue";
import { QuestionControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";

const layout = ref("inline");
const form = reactive({
  name: "",
  post: "",
  isRead: false,
});
const onPageChange = (page: number) => {
  listParams.current = page;
};
const router = useRouter();
const practice = (recordId: number) => {
  router.push({
    path: `/question/do/${recordId}`,
  });
};
const serchQuestion = () => {
  listParams.current = 1;
  loadList();
};
const show = ref(true);
let listParams = reactive({
  title: undefined,
  tags: undefined,
  current: 1,
  pageSize: 7,
});
const list = ref([]);
const totalNum = ref(0);
const loadList = async () => {
  const res = await QuestionControllerService.listQuestionVoByPageUsingPost({
    current: 0,
    pageSize: 1000,
    title: listParams.title,
    tags: listParams.tags,
    sortField: "createTime",
    sortOrder: "descend",
  });

  if (res.code === 0) {
    list.value = res.data.records;
    totalNum.value = res.data.total;
  } else {
    message.error(res.message);
  }
};
onMounted(() => {
  loadList();
});

watchEffect(() => {
  loadList();
});

const columns = [
  {
    title: "ID",
    dataIndex: "id",
  },
  {
    title: "题目",
    dataIndex: "title",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "通过率",
    slotName: "submitRate",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];
</script>
